package cn.gdut.chengcai.base;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;

import cn.gdut.chengcai.entity.MemberInfo;
import cn.gdut.chengcai.service.MemberInfoService;

public class AccountUtil {
	public static MemberInfo getCurrentAcount(HttpSession session, MemberInfoService service) {
		Long uid = getUid(session);
		if (StringUtils.isEmpty(uid.toString()) == true) {
			return null;
		}

		MemberInfo entity = service.selectByPrimaryKey(uid);
		return entity;
	}

	public static String getFromSession(HttpSession session, String key) {
		String value = (String) session.getAttribute(key);
		if (StringUtils.isEmpty(value) == true) {
			return null;
		}
		return value;
	}

	public static Long getUid(HttpSession session) {
		MemberInfo member = (MemberInfo) session.getAttribute(ConstantUtil.STR_MEMBER);
		return member == null ? null : member.getId();
	}

	/*public static String getUserSnFromSession(HttpSession session) {
		String value = getFromSession(session, ConstantUtil.STR_USER_SN);
		return value == null ? null : value;
	}
	
	public static Integer getUserTypeFromSession(HttpSession session) {
		String value = getFromSession(session, ConstantUtil.STR_USER_TYPE);
		return value == null ? null : Integer.valueOf(value);
	}
	
	public static String getUsernameFromSession(HttpSession session) {
		return getFromSession(session, ConstantUtil.STR_USERNAME);
	}
	
	public static String getNicknameFromSession(HttpSession session) {
		return getFromSession(session, ConstantUtil.STR_NICKNAME);
	}*/

	/**
	 * 
	 * 根据ID删除记录，真正的删除，小心使用！！！
	 */
	/*public static void deleteById(MemberInfo condition, IBaseService service, HttpSession session,
			ResponseData<Object> result) {
		IBaseEntity entity = service.findOneByCondition(condition);
		if (entity != null) {
			service.deleteById(entity.getId());
			result.setIsSuccess(true);
		} else {
			result.setMessage(ErrorMessageUtil.DELETE_FAILURE);
			result.setIsSuccess(false);
		}
	}*/

	/**
	 * 
	 * 根据删除标记来删除记录，逻辑上的删除
	 */
	/*public static void deleteByLabel(IBaseEntity condition, IBaseService service, HttpSession session,
			ResponseData<Object> result) {
		IBaseEntity entity = service.findOneByCondition(condition);
		if (entity != null) {
			entity.setIsDeleted(true);
			service.updateById(entity);
			result.setIsSuccess(true);
		} else {
			result.setMessage(ErrorMessageUtil.DELETE_FAILURE);
			result.setIsSuccess(false);
		}
	}*/
}
